/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InterfazCrearAsignatura.java
 *
 * Created on 18/10/2011, 01:27:08 PM
 */

package presentacion;

import Paneles.PanelGeneral;
import java.awt.BorderLayout;
import javax.swing.DefaultComboBoxModel;
import java.util.List;
import business.AreaBO;
import business.AsignaturaBO;
import dominio.Asignatura;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
/**
 *
 * @author eagle
 */
public class InterfazCrearAsignatura extends javax.swing.JFrame {

private AreaBO areaBo = new AreaBO();
private int ok;
private AsignaturaBO asignaturaBo = new AsignaturaBO();
private ImageIcon icono;


    /** Creates new form InterfazCrearAsignatura */
    public InterfazCrearAsignatura() {
        initComponents();
        PanelGeneral panel=new PanelGeneral();
        this.add(panel,BorderLayout.SOUTH);
        icono = new ImageIcon(getClass().getResource("/Imagenes/icon.gif"));
        this.setIconImage(icono.getImage());

        cargarListaGrados();
        cargarListaAreas();
        cargarListaIntensidadHoraria();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        entradaNombreAsignatura = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        comboGrados = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        comboAreas = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        comboIntensidadHoraria = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setTitle("Creación de Asignaturas");
        setResizable(false);

        jLabel1.setText("Nombre de la asignatura:");

        jLabel2.setText("Grado al que se dicta:");

        comboGrados.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel3.setText("Área:");

        comboAreas.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel4.setText("Intensidad Horaria (en horas a la semana)");

        comboIntensidadHoraria.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jButton1.setText("Crear Asignatura");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(103, 103, 103)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel3)
                                .addComponent(jLabel2)
                                .addComponent(jLabel1))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(comboAreas, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(comboGrados, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(entradaNombreAsignatura, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE)))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(25, 25, 25)
                            .addComponent(jLabel4)
                            .addGap(18, 18, 18)
                            .addComponent(comboIntensidadHoraria, 0, 244, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(58, 58, 58)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(48, 48, 48)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(34, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(entradaNombreAsignatura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(comboGrados, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(comboAreas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(comboIntensidadHoraria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(33, 33, 33))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        ok = 0;
        Asignatura tst;

        if (entradaNombreAsignatura.getText().equals("") || comboIntensidadHoraria.getSelectedIndex()==0 || comboGrados.getSelectedIndex()==0) {
            JOptionPane.showMessageDialog(this, "Debe digitar un nombre, seleccionar una Intensidad Horaria y escoger un grado", null, JOptionPane.ERROR_MESSAGE);
        }
        else {
            tst = asignaturaBo.consultarAsignatura(entradaNombreAsignatura.getText()+"", Integer.parseInt(comboGrados.getSelectedIndex()+""));
          
            if (tst == null) {
                ok = asignaturaBo.crearAsignatura(0,entradaNombreAsignatura.getText(),comboIntensidadHoraria.getSelectedIndex(),comboGrados.getSelectedIndex()-1,areaBo.encontrarArea(comboAreas.getSelectedItem()+""),0);
                entradaNombreAsignatura.setText("");
            }
            if (ok == 1) {
                JOptionPane.showMessageDialog(this, "La creación de la asignatura fue exitosa", "Creación exitosa !!!", JOptionPane.INFORMATION_MESSAGE);
            }
            else {
                if (tst != null) {
                    JOptionPane.showMessageDialog(this, "Ocurrio un error al crear la asignatura (Nombres repetidos)", "Creación fallida", JOptionPane.ERROR_MESSAGE);
                }
                else {
                    JOptionPane.showMessageDialog(this, "Ocurrio un error al crear la asignatura", "Creación fallida", JOptionPane.ERROR_MESSAGE);
                }
            }
            // this.setVisible(false);t
            cargarListaAreas();
        }







    }//GEN-LAST:event_jButton1ActionPerformed

    private void cargarListaGrados(){
    DefaultComboBoxModel modelo = (DefaultComboBoxModel) comboGrados.getModel();
    modelo.removeAllElements();
    modelo.addElement("Seleccione");
    modelo.addElement("0.  Transicion");
    modelo.addElement("1.  Primero");
    modelo.addElement("2.  Segundo");
    modelo.addElement("3.  Tercero");
    modelo.addElement("4.  Cuarto");
    modelo.addElement("5.  Quinto");
    modelo.addElement("6.  Sexto");
    modelo.addElement("7.  Séptimo");
    modelo.addElement("8.  Octavo");
    modelo.addElement("9.  Noveno");
    modelo.addElement("10.  Decimo");
    modelo.addElement("11.  Once");
    }

    private void cargarListaAreas()
    {
        List l = areaBo.consultarAreas();
        DefaultComboBoxModel modelo = (DefaultComboBoxModel) comboAreas.getModel();
        modelo.removeAllElements();
        for (int i = 0; i < l.size(); i++) {
            modelo.addElement(l.get(i));

        }
        comboAreas.setModel(modelo);
    }

    private void cargarListaIntensidadHoraria(){
    DefaultComboBoxModel modelo = (DefaultComboBoxModel) comboIntensidadHoraria.getModel();
    modelo.removeAllElements();
    modelo.addElement("Seleccione");
    modelo.addElement("1 Hora");
    modelo.addElement("2 Horas");
    modelo.addElement("3 Horas");
    modelo.addElement("4 Horas");
   }


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new InterfazCrearAsignatura().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboAreas;
    private javax.swing.JComboBox comboGrados;
    private javax.swing.JComboBox comboIntensidadHoraria;
    private javax.swing.JTextField entradaNombreAsignatura;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    // End of variables declaration//GEN-END:variables

}
